package com.example.demo.pay.handler.impl;

import com.example.demo.enumclass.NotificationProcessStatusEnum;
import com.example.demo.mq.bo.ScoreMqBo;
import com.example.demo.mq.producer.ScoreMonitorProducer;
import com.example.demo.pay.handler.PayCallbackHandler;
import com.example.demo.paymqnotification.entity.PayMqNotificationRecord;
import com.example.demo.paymqnotification.service.IPayMqNotificationRecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Component
@Slf4j
@AllArgsConstructor
public class PayCallbackHandlerImpl implements PayCallbackHandler {

    private IPayMqNotificationRecordService iPayMqNotificationRecordService;
    private ScoreMonitorProducer scoreMonitorProducer;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void payCallback(String orderNo) {
        //TODO　处理其他业务


        //记录通知信息
        PayMqNotificationRecord payMqNotificationRecord = new PayMqNotificationRecord();
        payMqNotificationRecord.setOrderNo(orderNo);
        payMqNotificationRecord.setCreateTime(LocalDateTime.now());
        payMqNotificationRecord.setStatus(NotificationProcessStatusEnum.Padding.getStatus());
        iPayMqNotificationRecordService.save(payMqNotificationRecord);

        //发送mq
        ScoreMqBo scoreMqBo = new ScoreMqBo();
        scoreMqBo.setStatus(NotificationProcessStatusEnum.Padding.getStatus());
        scoreMqBo.setOrderNo(orderNo);
        scoreMonitorProducer.sendPayScoreChangeNotification(scoreMqBo);
    }
}
